package com.jinlejia.bbs.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.bean.base.TagBean;
import com.jinlejia.bbs.databinding.BbsAdapterInvitedUserItemBinding;
import com.jinlejia.bbs.ui.mine.others.BBSOtherUserMainActivity;

import java.util.ArrayList;

/**
 * @author 王进 on 2019/3/20.
 */

public class InvitedUserAdapter extends BaseLoadAdapter<ArrayList<BBSUserBean>>{
    private final int TYPE_DATA = 1;
    private final String topicCode;
    public InvitedUserAdapter(Context context,String topicCode) {
        super(context);
        this.topicCode = topicCode;
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType){
            case TYPE_DATA:
                viewHolder = new ItemViewHolder(inflater.inflate(R.layout.bbs_adapter_invited_user_item, parent, false));
                break;
            default:
                throw new IllegalArgumentException("未知类型");
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder){
            bindItemViewHolder((ItemViewHolder) holder,position);
        }
    }

    public interface InvitedResultListener{
        void onResult(BBSUserBean result,String topicCode);
    }
    private InvitedResultListener invitedResultListener;

    public void setInvitedResultListener(InvitedResultListener invitedResultListener) {
        this.invitedResultListener = invitedResultListener;
    }

    @Override
    public int getItemCount() {
        if (mData==null || mData.size()==0){
            return 1;
        }
        return mData.size();
    }

    @Override
    protected int getItemViewTypeZ(int position) {
        return TYPE_DATA;
    }

    private static class ItemViewHolder extends RecyclerView.ViewHolder{
        ItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    private void bindItemViewHolder(ItemViewHolder holder,int position){
        final BBSUserBean bean = mData.get(position);
        BbsAdapterInvitedUserItemBinding binding= DataBindingUtil.getBinding(holder.itemView);
        binding.setUser(bean);
        ArrayList<TagBean> tags = bean.getTags();
        if (tags!=null){
            binding.tagGroupFlexboxLayout.removeAllViews();
            for (TagBean tb:tags) {
                TextView textView = new TextView(mContext);
                textView.setTextColor(Color.parseColor("#FCCB00"));
                textView.setText(tb.getValueName());
                textView.setBackgroundResource(R.drawable.bbs_shape_search_tag_bg2);
                textView.setTextSize(10);
                binding.tagGroupFlexboxLayout.addView(textView);
            }

        }
        binding.userAvatarImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, BBSOtherUserMainActivity.class);
                intent.putExtra("pin",bean.getUserPin());
                mContext.startActivity(intent);
            }
        });
        binding.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (invitedResultListener!=null){
                    if (mContext instanceof Activity){
                        if (!((Activity) mContext).isFinishing()){
                            invitedResultListener.onResult(bean,topicCode);
                        }
                    }
                }
            }
        });
        if (position==mData.size()-1){
            binding.lineView.setVisibility(View.GONE);
        }else {
            binding.lineView.setVisibility(View.VISIBLE);
        }
    }

}
